package com.briup.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.briup.comon.R;
import com.briup.entity.Category;
import com.briup.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Auther: liao
 * @Date: 2022/9/12-09-12-12:00
 * @Description：分类管理
 */
@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
    
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param category
     * @return
     */
    
    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("请求保存菜品！");

        boolean save = categoryService.save(category);


        return R.success("新增分类成功");
    }

    /**
     * 分页
     * @param page
     * @param pageSize
     * @return
     */
    
    
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize){
        log.info("请求到分类菜品的分页，page={},pageSize={}",page,pageSize);
        
        //1.先造出分页构造器
        Page<Category> pageInfo = new Page<>(page,pageSize);
        
        //2.条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        //3.执行的条件
        queryWrapper.orderByAsc(Category::getSort);
        
        //3.执行语句
        Page page1 = categoryService.page(pageInfo, queryWrapper);

        return R.success(page1);
    }

    /**
     * 根据id修改分类信息
     * @param category
     * @return
     */
    
    @PutMapping
    public R<String> update(@RequestBody Category category){
        log.info("请求到修改菜品{}",category);
        
        categoryService.updateById(category);
        
        return R.success("修改分类信息成功");
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    
    @DeleteMapping
    //这里为什么不能使用下面哪个注解，要使用下面那个注解是没有那种问号的，比如；delete/16 像这种就可以使用@Pathvariable
    //@DeleteMapping("/{id}")
    
    //这里使用的是category?id=15454564212315  像这种是普通参数的传递，直接在方法里面接收就可以了
    
    public  R<String> delete( Long id){
        log.info("请求到删除菜品id：{}",id);
        
        //categoryService.removeById(id);
        categoryService.deleteById(id);
        
        return R.success("删除成功!");
        
    }

    /**
     * 通过菜品类型查询所有分类
     * @param category
     * @return
     */
    
    @GetMapping("/list")
    public R<List<Category>> selectAllByType(Category category){
        
        //1.条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        
        //执行语句
        List<Category> list = categoryService.list(queryWrapper);

        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        return R.success(list);   
    }
    
    
    
}
